package com.scm.biz.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scm.biz.bean.DesignOrder;
import com.scm.biz.bean.Order;
import com.scm.biz.dto.DesignOrderDto;
import com.scm.biz.query.DesignOrderQuery;
import com.scm.biz.result.DesignOrderResult;
import com.scm.biz.service.CustomerService;
import com.scm.biz.service.DesignOrderService;
import com.scm.biz.service.OrderPatternService;
import com.scm.biz.service.OrderService;
import com.scm.biz.service.OrderTaskFlowService;
import com.scm.biz.service.SysUserService;
import com.scm.core.bean.exp.UfdmException;
import com.scm.core.bean.resp.RespBean;
import com.scm.utils.Const;
import com.scm.utils.UfdmDateUtil;

@Controller
@RequestMapping("/designorder")
public class DesignOrderAction {
	
	@Resource
	private DesignOrderService designOrderService;

	@Resource
	private CustomerService customerService;
	@Resource
	private SysUserService sysUserService;
	@Resource
	OrderTaskFlowService orderTaskFlowService;
	@Resource
	OrderPatternService orderPatternService;
	@Resource
	OrderService orderService;
	@RequestMapping("/addPage")
	public ModelAndView designOrderAddPage(Integer customerId){
		
		ModelAndView mv = new ModelAndView("order/design_add");
		mv.addObject("permisionCode", "F101");
		mv.addObject("customerList", customerService.selectList());
		mv.addObject("customerId", customerId);
		return mv;
	}
	
	
	@RequestMapping("/add")
	@ResponseBody
	public RespBean<String> add(DesignOrderDto designOrderDto)throws Exception{
		RespBean<String> respBean = new RespBean<>();
		//纯数字正则判断
		String regex = "^\\d*$";
		if(designOrderDto.getCustomerId()==null){
			throw new UfdmException("客户名称不能为空");
		}
		if(designOrderDto.getDesignEnddate()==null||designOrderDto.getDesignEnddate().isEmpty()){
			throw new UfdmException("设计截止时间不能为空");
		}
		
		if(designOrderDto.getDesignCount()==null||designOrderDto.getDesignCount().isEmpty()){
			throw new UfdmException("设计款数不能为空");
		}
		if(!designOrderDto.getDesignCount().matches(regex)){
			throw new UfdmException("设计款数只能是纯数字");
		}
	
		DesignOrder designOrder = new DesignOrder();
		Order order = new Order();
		order.setCode(Const.getOrderCode());
		order.setCustomerId(designOrderDto.getCustomerId());
		//
		Integer userId = Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString());
		order.setCreator(userId);
		
		
		designOrder.setDesignEnddate(UfdmDateUtil.stringToDate(designOrderDto.getDesignEnddate(), "yyyy-MM-dd HH:mm"));
		if(designOrder.getDesignEnddate() == null){
			throw new UfdmException("设计截止时间不能为空");
		}
		designOrder.setProofingEnddate(UfdmDateUtil.stringToDate(designOrderDto.getProofingEnddate(), "yyyy-MM-dd HH:mm"));
		
		designOrder.setDesignCount(Integer.parseInt(designOrderDto.getDesignCount()));
		designOrder.setRequirement(designOrderDto.getRequirement());
		designOrder.setCreator(userId);
		designOrderService.addDesignOrder(designOrder, order);//指定下一个步骤为指派任务
		return respBean;
	}
	@RequestMapping("/submitverify")
	@ResponseBody
	public RespBean<String> submitVerify(Long businessId,Long orderId)throws Exception{
		RespBean<String> respBean = new RespBean<>();
		DesignOrder designOrder = designOrderService.selectById(businessId);
		if(designOrder.getDesignCount() !=orderPatternService.selectListByOrderId(orderId).size()){
			throw new UfdmException("上传设计稿件数与设计订单中的设计件数不一致");
		}
		designOrderService.saveSubmitVerify(businessId, orderId);
		return respBean;
	}
	@RequestMapping("/detail")
	public ModelAndView detail(Long businessId,Long orderId){
		DesignOrderResult order=designOrderService.selectById(businessId);
		ModelAndView mv = new ModelAndView("order/design_order_detail");
		mv.addObject("businessId", businessId);
		mv.addObject("orderId", orderId);
		mv.addObject("order", order);
		mv.addObject("designerList", sysUserService.selectListByRoleCode("R006"));//设计师
		mv.addObject("managerList", sysUserService.selectListByRoleCode("R003"));//项目经理
		return mv;
	}
	
	@RequestMapping("/verify")
	@ResponseBody
	public RespBean<String> verify(Long businessId,Long orderId)throws Exception{
		RespBean<String> respBean = new RespBean<>();
		designOrderService.saveVerify(businessId, orderId);
		return respBean;
	}
	@RequestMapping("/closeorder")
	@ResponseBody
	public RespBean<String> closeOrder(Long businessId,Long orderId)throws Exception{
		RespBean<String> respBean = new RespBean<>();
	
		designOrderService.saveClose(businessId, orderId);
		return respBean;
	}
	
	/**
	 * 转发到设计订单列表页
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView designOrderListPage(DesignOrderQuery designOrderQuery,HttpServletRequest req){
		if(designOrderQuery.getPageNo()==0){
			designOrderQuery.setPageNo(1);
		}
		ModelAndView mv = new ModelAndView("order/design_list");
		designOrderQuery.setPerformer(Integer.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId").toString()));
		mv.addObject("orderList", designOrderService.selectPage(designOrderQuery));
		designOrderQuery.setRecourdCount(designOrderService.selectTotal(designOrderQuery));
		mv.addObject("status", designOrderQuery.getStatus());
		mv.addObject("designerList", sysUserService.selectListByRoleCode("R006"));//设计师
		mv.addObject("managerList", sysUserService.selectListByRoleCode("R003"));//项目经理
		mv.addObject("code", designOrderQuery.getCode());
		mv.addObject("customerName", designOrderQuery.getCustomerName());
		mv.addObject("isExceed", designOrderQuery.getIsExceed());
		mv.addObject("splitPage", designOrderQuery.getSplitPage());
		mv.addObject("path", Const.getPath(req));
		//获取
		return mv;
	}
	
	@RequestMapping("/assignDesigner")
	@ResponseBody
	public RespBean<String> assignDesigner(String businessIds,Integer designer)throws Exception{

		RespBean<String> respBean = new RespBean<>();
		List<Long> businessIdList = new ArrayList<Long>();
		for(String businessId:businessIds.split(",")){
			if(businessId !=null && !businessId.isEmpty()){
				businessIdList.add(Long.valueOf(businessId));
			}
		}
		designOrderService.saveAssignUser(businessIdList,designer);
		return respBean;
	}
	
}
